package com.mcr.reggie.controller;

import com.mcr.reggie.common.R;
import com.mcr.reggie.entity.User;
import com.mcr.reggie.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@Api(tags = "用户登陆注册接口")
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @ApiOperation(value = "用户请求发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户信息请求体",required = true)
    })
    @PostMapping(value = "/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        String code = userService.sentMsgByQQMail(user);
        if(code!=null){
            return R.success("验证码："+code);
        }
        return R.error("qq邮箱号为空");
    }
    @ApiOperation("用户请求注册/登录")
    @PostMapping(value = "login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        User user=userService.login(map,session);
        if(user!=null){
            return R.success(user);
        }
        return R.error("验证码错误！！");
    }
    @ApiOperation(value = "退出登录")
    @PostMapping("/loginout")
    public R<String> loginOut(HttpSession session){
        session.removeAttribute("user");
        return R.success("已退出登录");
    }
}
